package com.ccp.dev.system.model;

import com.ccp.dev.core.basic.model.BaseModel;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Component;

/**
 * 对象功能:系统角色表 Model对象
 * @author y
 */
@Component
public class SysRole extends BaseModel implements GrantedAuthority {

	/**
	 * 公开角色标记 isPublic = 1  代表公开
	 */
	public final static Short DEFAULT_PUBLICROLE = 1;
	/**
	 * 三员角色标记 isPublic = 2  代表三员
	 */
	public final static Short DEFAULT_THREEROLE = 2;
	/**
	 * 系统管理员
	 */
	public final static Short DEFAULT_XTGLY = 1;
	/**
	 * 安全保密管理员
	 */
	public final static Short DEFAULT_AQBMGLY = 2;
	/**
	 * 安全审计员
	 */
	public final static Short DEFAULT_AQSJY = 3;
	/**
	 * 启用
	 */
	public final static Short ENABLED_Y = 1;
	/**
	 * 禁用
	 */
	public final static Short ENABLED_N = 0;
	/**
	 * 允许删除
	 */
	public final static Short ALLOWDEL_Y = 1;
	/**
	 * 不允许删除
	 */
	public final static Short ALLOWDEL_N = 0;
	/**
	 * 允许编辑
	 */
	public final static Short AALLOWEDIT_Y = 1;
	/**
	 * 不允编辑
	 */
	public final static Short ALLOWEDIT_N = 0;

	/**
	 * 当前系统
	 */
	public static String CURRENT_SYSROLE="CURRENT_SYSROLE";

	/**
	 * 超级
	 */
	private final static String ROLE_SUPER  = "ROLE_SUPER";
	/**
	 * 公共角色
	 */
	private final static String ROLE_PUBLIC  = "ROLE_PUBLIC";
	/**
	 * 匿名级
	 */
	private final static String ROLE_ANONYMOUS  = "ROLE_ANONYMOUS";
	/**
	 * REST
	 */
	private final static String ROLE_REST  = "ROLE_REST";
	/**
	 * NONE
	 */
	private final static String ROLE_NONE  = "ROLE_NONE";
	/**
	 * THREE
	 */
	private final static String ROLE_THREE  = "ROLE_THREE";
	
	public final static GrantedAuthority ROLE_GRANT_SUPER=new SimpleGrantedAuthority(SysRole.ROLE_SUPER);
	public final static ConfigAttribute ROLE_CONFIG_PUBLIC=new SecurityConfig(SysRole.ROLE_PUBLIC);
	public final static ConfigAttribute ROLE_CONFIG_ANONYMOUS=new SecurityConfig(SysRole.ROLE_ANONYMOUS);
	public final static ConfigAttribute ROLE_CONFIG_REST=new SecurityConfig(SysRole.ROLE_REST);
	public final static ConfigAttribute ROLE_CONFIG_NONE=new SecurityConfig(SysRole.ROLE_NONE);
	public final static ConfigAttribute ROLE_CONFIG_THREE=new SecurityConfig(SysRole.ROLE_THREE);


	/**
	 * 角色id
	 */
	private String roleId;
	/**
	 * 系统ID  如果为0 则为全局角色
	 */
	private String systemId;
	/**
	 * 角色别名
	 */
	private String alias;
	/**
	 * 角色名
	 */
	private String roleName;
	/**
	 * 备注
	 */
	private String memo;
	/**
	 * 允许删除  1允许 0不允许
	 */
	private Short allowDel;
	/**
	 * 允许编辑  1允许 0不允许
	 */
	private Short allowEdit;
	/**
	 * 是否启用   1 是启用  0是禁用
	 */
	private Short enabled;
	/**
	 * 是否可切换 1是 0 否
	 */
	private Short enableSwitch;
	/**
	 * 是否可公开,0:普通；1:公开；2:三员
	 */
	private Short isPublic;
	/**
	 * 角色分类
	 */
	private String category;
	/**
	 * 角色类型  1:系统管理员 2:安全保密管理员  3：安全审计员    三员角色专用字段
	 */
	private Short roleType;

	/**
	 * 子系统对象
	 */
	private SubSystem subSystem;
	/**
	 * 系统名称
	 */
	private String systemName;
	/**
	 * 是否父节点
	 */
	private String isParent="false";
	/**
	 * 是否可以分配资源
	 */
	private Short allowSourceRole;
	/**
	 * 是否可以分配人员
	 */
	private Short allowAssignUser=1;
	/**
	 * 组织id
	 */
	private String orgId;

	public String getOrgId() {
		return orgId;
	}

	public void setOrgId(String orgId) {
		this.orgId = orgId;
	}

	public Short getAllowSourceRole() {
		return allowSourceRole;
	}

	public void setAllowSourceRole(Short allowSourceRole) {
		this.allowSourceRole = allowSourceRole;
	}
	
	public Short getAllowAssignUser() {
		return allowAssignUser;
	}

	public void setAllowAssignUser(Short allowAssignUser) {
		this.allowAssignUser = allowAssignUser;
	}

	public void setAlias(String alias)
	{
		this.alias = alias;
	}

	public String getAlias()
	{
		return alias;
	}

	public void setRoleName(String roleName)
	{
		this.roleName = roleName;
	}

	public String getRoleName()
	{
		return roleName;
	}

	public void setMemo(String memo)
	{
		this.memo = memo;
	}

	public String getMemo()
	{
		return memo;
	}

	public void setAllowDel(Short allowDel)
	{
		this.allowDel = allowDel;
	}

	public Short getAllowDel()
	{
		return allowDel;
	}

	public void setAllowEdit(Short allowEdit)
	{
		this.allowEdit = allowEdit;
	}

	public Short getAllowEdit()
	{
		return allowEdit;
	}

	public void setEnabled(Short enabled)
	{
		this.enabled = enabled;
	}
	
	public SubSystem getSubSystem() {
		return subSystem;
	}

	public void setSubSystem(SubSystem subSystem) {
		this.subSystem = subSystem;
	}

	public String getSystemName() {
		return systemName;
	}

	public void setSystemName(String systemName) {
		this.systemName = systemName;
	}

	public String getRoleId() {
		return roleId;
	}

	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}

	public String getSystemId() {
		return systemId;
	}

	public void setSystemId(String systemId) {
		this.systemId = systemId;
	}

	/**
	 * 返回 是否启用
	 * 
	 * @return
	 */
	public Short getEnabled()
	{
		return enabled;
	}

	public String getIsParent() {
		return isParent;
	}

	public void setIsParent(String isParent) {
		this.isParent = isParent;
	}

	
	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}
	
	public Short getEnableSwitch() {
		return enableSwitch;
	}

	public void setEnableSwitch(Short enableSwitch) {
		this.enableSwitch = enableSwitch;
	}
	

	public Short getIsPublic() {
		return isPublic;
	}

	public void setIsPublic(Short isPublic) {
		this.isPublic = isPublic;
	}

	public Short getRoleType() {
		return roleType;
	}

	public void setRoleType(Short roleType) {
		this.roleType = roleType;
	}

	@Value("${loginRole.cookie.name}")
	public void setCurrentSysrole(String currentSysrole) {
		CURRENT_SYSROLE = currentSysrole;
	}
	/**
	 * @see Object#equals(Object)
	 */
	@Override
	public boolean equals(Object object)
	{
		if (!(object instanceof SysRole))
		{
			return false;
		}
		SysRole rhs = (SysRole) object;
		return new EqualsBuilder()
				.append(this.alias, rhs.alias)
				.append(this.tenantId, rhs.tenantId)
				.isEquals();
	}

	/**
	 * @see Object#hashCode()
	 */
	@Override
	public int hashCode()
	{
		return new HashCodeBuilder(-82280557, -700257973)
				.append(this.roleId)
				.append(this.systemId)
				.append(this.alias)
				.append(this.roleName)
				.append(this.memo)
				.append(this.allowDel)
				.append(this.allowEdit)
				.append(this.enabled)
				.append(this.category)
				.append(this.isPublic)
				.toHashCode();
	}

	/**
	 * @see Object#toString()
	 */
	@Override
	public String toString()
	{
		return new ToStringBuilder(this)
				.append("roleId", this.roleId)
				.append("systemId", this.systemId)
				.append("alias", this.alias)
				.append("roleName", this.roleName)
				.append("memo", this.memo)
				.append("allowDel", this.allowDel)
				.append("allowEdit", this.allowEdit)
				.append("enabled", this.enabled)
				.append("enabled", this.category)
				.append("tenantId", this.tenantId)
				.append("isPublic", this.isPublic)
				.toString();
	}

	@Override
	public String getAuthority()
	{
		return alias;
	}
}